<template>
    <div class="weixin-tip" v-show="weixinTips">
        <p>
            <img src="../assets/images/live_weixin.png" alt="微信打开" />
        </p>
    </div>
</template>

<script>
export default {
    name: "openApp",
    props: {
        weixinTips: {
            default: false
        }
    },
    data () {
        return {
            iphoneSchema: 'gemallYJG://', //首页？
            iphoneShopSchema: 'gemallYJG://appShopShareLink?',
            iphoneGoodsSchema: 'gemallYJG://appGoodsShareLink?',
            iphonePromotionSchema: 'gemallYJG://promotion?',
            iphoneYjgActivitySchema: 'gemallYJG://appGoodsShareLink?',
            iphoneBackSchema: 'gemallyjg://back',
            // iphoneDownUrl: 'https://itunes.apple.com/cn/app/%E5%A3%B9%E9%94%AE%E5%93%A5/id1271689594?mt=8',
            iphoneDownUrl: 'http://www.1-touchpay.com/#/weiYiJianGe',
            androidSchema: 'gemallyjg://com.gatewang.yjg:80/YJGMainActivity', //
            androidShopSchema: 'yjgshare://com.gatewang.yjg/appShopShareLink?',
            androidGoodsSchema: 'yjgshare://com.gatewang.yjg/appGoodsShareLink?',
            androidPromotionSchema: 'gemallyjg://com.gatewang.yjg/promotion?',
            androidYjgActivitySchema: 'yjgshare://com.gatewang.yjg/appGoodsShareLink?',
            androidBackSchema: 'gemallyjg://back',
            // androidDownUrl: 'http://android.myapp.com/myapp/detail.htm?apkName=com.gatewang.yjg',
            androidDownUrl: 'http://www.1-touchpay.com/#/weiYiJianGe'

        }
    },
    created: function () {
        if (this.$route.query.auto) {
            this.openApp();
        }
    },
    methods: {
        /**
         * @param options array 链接参数
         * @param type string 来源类型 
         * @param openDownLoad bool 是否尝试打开壹键哥下载地址
         */
        openApp (options = [], type = '', openDownLoad = true) {
            let this_ = this
            // 微信
            if (this_.isWeixin() || this_.isQQ()) {
                this.weixinTips = true;
            } else { // 非微信浏览器
                if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
                    if (type == 'goods') {
                        window.location = this.generateScheme(this.iphoneGoodsSchema, options)
                    } else if (type == 'shop') {
                        window.location = this.generateScheme(this.iphoneShopSchema, options)
                    } else if (type == 'promotion') {
                        window.location = this.generateScheme(this.iphonePromotionSchema, options)
                    } else if (type == 'yjgActivity') {
                        window.location = this.generateScheme(this.iphoneYjgActivitySchema, options)
                    } else if (type == 'TyActivity') {
                        window.location = this.generateScheme(this.iphoneYjgActivitySchema, options)
                    } else if (type == 'GqActivity') {
                        window.location = this.generateScheme(this.iphoneYjgActivitySchema, options)
                    } else if (type == 'back') {
                        window.location = this.iphoneBackSchema
                    } else {
                        window.location = this.iphoneSchema // 旧处理方式
                    }
                    if (!openDownLoad) return true;
                    let loadDateTime = new Date()
                    window.setTimeout(function () {
                        let timeOutDateTime = new Date()
                        if (timeOutDateTime - loadDateTime < 5000) {
                            window.location = this_.iphoneDownUrl // ios下载地址
                        } else {
                            window.close()
                        }
                    }, 2000)
                } else if (navigator.userAgent.match(/android/i)) {
                    try {
                        if (type == 'goods') {
                            window.location = this.generateScheme(this.androidGoodsSchema, options)
                        } else if (type == 'shop') {
                            window.location = this.generateScheme(this.androidShopSchema, options)
                        } else if (type == 'promotion') {
                            window.location = this.generateScheme(this.androidPromotionSchema, options)
                        } else if (type == 'yjgActivity') {
                            window.location = this.generateScheme(this.androidYjgActivitySchema, options)
                        } else if (type == 'TyActivity') {
                            window.location = this.generateScheme(this.androidYjgActivitySchema, options)
                        } else if (type == 'GqActivity') {
                            window.location = this.generateScheme(this.androidYjgActivitySchema, options)
                        } else if (type == 'back') {
                            window.location = this.androidBackSchema
                        } else {
                            window.location = this_.androidSchema // 旧处理方式
                        }
                        if (!openDownLoad) return true;
                        setTimeout(() => {
                            window.location = this_.androidDownUrl // android下载地址
                        }, 2000)
                    } catch (e) {
                        console.log(e)
                    }
                } else {
                    alert('请使用壹键哥或手机浏览器打开')
                }
            }
        },
        isWeixin () { // 判断是否是微信
            let ua = navigator.userAgent.toLowerCase()
            if (ua.indexOf('micromessenger') > 0) {
                return true
            } else {
                return false
            }
        },
        isQQ () {
            let ua = navigator.userAgent
            if (ua.match(/\sQQ/i) !== null) {
                return true
            } else {
                return false
            }
        },
        /* 生成带参完整scheme */
        generateScheme (baseUrl, options) {
            let urlScheme = baseUrl
            for (let item of options) {
                urlScheme = urlScheme + item.field + '=' + encodeURIComponent(item.value) + '&'
            }
            urlScheme = urlScheme.substring(0, urlScheme.length - 1)
            // console.log('urlScheme:', urlScheme)
            // return encodeURIComponent(urlScheme)
            return urlScheme
        }
    }
}
</script>

<style scoped>
.weixin-tip {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  filter: alpha(opacity=80);
  height: 100%;
  width: 100%;
  z-index: 100;
}

.weixin-tip p {
  text-align: center;
  margin-top: 10%;
  padding: 0 5%;
}

.weixin-tip {
  max-width: 100%;
  height: auto;
}
.weixin-tip img {
  width: 100%;
}
</style>

